/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NuevoAdministrador.java
 *
 * Created on 26/05/2010, 10:46:10 PM
 */

package ui;

import acceso.Administrador;
import gestion.GestionSpring;
import interfaz.InterfaceEjp;
import java.util.Date;
import javax.swing.JOptionPane;
import manejoExcepciones.ExcepcionesAdministrador;
import seguridad.SHA1BASE64;

/**
 *
 * @author Adriano
 */
public class NuevoAdministrador extends javax.swing.JFrame {

    PaginaPrincipal miPaginaPrincipal;

    public void setMiPaginaPrincipal(PaginaPrincipal miPaginaPrincipal) {
        this.miPaginaPrincipal = miPaginaPrincipal;
    }


    /** Creates new form NuevoAdministrador */
    public NuevoAdministrador() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        UsuarioI = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        PasswordI1 = new javax.swing.JPasswordField();
        PasswordI2 = new javax.swing.JPasswordField();
        ComboPrivilegios = new javax.swing.JComboBox();
        BotonOk = new javax.swing.JButton();
        BotonCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Inversiones E.J.P ");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Nuevo Administrador"));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText("Nombre de usuario:");
        jLabel1.setName("jLabel1"); // NOI18N

        UsuarioI.setName("UsuarioI"); // NOI18N
        UsuarioI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UsuarioIActionPerformed(evt);
            }
        });

        jLabel2.setText("Password:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Repita su Password:");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("Privilegios:");
        jLabel4.setName("jLabel4"); // NOI18N

        PasswordI1.setName("PasswordI1"); // NOI18N
        PasswordI1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PasswordI1ActionPerformed(evt);
            }
        });

        PasswordI2.setName("PasswordI2"); // NOI18N
        PasswordI2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PasswordI2ActionPerformed(evt);
            }
        });

        ComboPrivilegios.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ninguno", "Todos" }));
        ComboPrivilegios.setName("ComboPrivilegios"); // NOI18N

        BotonOk.setText("OK");
        BotonOk.setName("BotonOk"); // NOI18N
        BotonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonOkActionPerformed(evt);
            }
        });

        BotonCancelar.setText("Cancelar");
        BotonCancelar.setName("BotonCancelar"); // NOI18N
        BotonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(BotonOk)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(BotonCancelar))
                    .addComponent(UsuarioI, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(PasswordI1, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(PasswordI2, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(ComboPrivilegios, 0, 149, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(UsuarioI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(PasswordI1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(PasswordI2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(ComboPrivilegios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BotonOk)
                    .addComponent(BotonCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-325)/2, (screenSize.height-237)/2, 325, 237);
    }// </editor-fold>//GEN-END:initComponents

    private void UsuarioIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UsuarioIActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_UsuarioIActionPerformed

    private void PasswordI1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PasswordI1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_PasswordI1ActionPerformed

    private void PasswordI2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PasswordI2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_PasswordI2ActionPerformed

    private void BotonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonCancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_BotonCancelarActionPerformed

    private void BotonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonOkActionPerformed

        try{

            //Verifica que los campos esten llenos
            manejoExcepciones.ExcepcionesAdministrador.
            CamposNoLlenos(UsuarioI.getText(), PasswordI1.getText(),
            PasswordI2.getText());
            //Verifica que los passwords sean iguales
            manejoExcepciones.ExcepcionesAdministrador.
            CompararPasswords(PasswordI1.getText(), PasswordI2.getText());
            //Verifica que el password tenga un minimo de 4 caracteres
            manejoExcepciones.ExcepcionesAdministrador.
            VerificarLongitudPasswordUsuario(PasswordI1.getText(),
            UsuarioI.getText());

           //Instanciacion de la interface.
           InterfaceEjp miInterfaceEjp = GestionSpring.getSingleton().MetodoGestion();
           //Verificamos que se a seleccionado en el combobox de privilegios
           int Seleccion =  ComboPrivilegios.getSelectedIndex();
           boolean TipoI = false;
               if (Seleccion == 0)
                   TipoI = false;
               else if (Seleccion == 1)
                   TipoI = true;
           //Enviamos a encriptar el password.
           SHA1BASE64 encrip = new SHA1BASE64();
           String passwordEncriptado = SHA1BASE64.encriptar(PasswordI1.getText());
           //Contruimos el objeto Administrador a insertar.
           Administrador miAdministrador = new Administrador (UsuarioI.getText(),
                   passwordEncriptado, new Date(), TipoI);
           miInterfaceEjp.registrarAdmin(miAdministrador);
           JOptionPane.showMessageDialog(this, "Insercion Exitosa");
           this.setVisible(false);


        }catch (ExcepcionesAdministrador EA1){

            JOptionPane.showMessageDialog(this, EA1.getMessage());

        }

    }//GEN-LAST:event_BotonOkActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NuevoAdministrador().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonCancelar;
    private javax.swing.JButton BotonOk;
    private javax.swing.JComboBox ComboPrivilegios;
    private javax.swing.JPasswordField PasswordI1;
    private javax.swing.JPasswordField PasswordI2;
    private javax.swing.JTextField UsuarioI;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

}
